/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.soa.service.busi.wx;

import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.lianzt.factory.AESFactory;
import com.soa.exception.GlobalException;
import com.soa.service.BaseService;
import static com.soa.service.BaseService.getSession;
import com.soa.util.SystemUtil;
import com.soa.util.tccgl.TccglUtil;
import java.io.IOException;
import java.util.logging.Level;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * P90001 绑定用户
 *
 * @author Administrator
 */
@Service
public class BindUser extends BaseService {

    final String[] KEY = {"username", "用户名", "password", "密码"};
    private final Logger log = LoggerFactory.getLogger(BindUser.class);
    @Resource
    private TccglUtil tccglUtil;

    @Override
    public String[] keys() {
        return KEY;
    }

    /**
     * 用于验证用户是否存在，存在的话，加入到wx_user中
     */
//    get_duser_auth=select * from pl_user where username=？ and password=?
//    update_ygxx_userid=update pl_user set openid=? where username=?
    @Override
    @Transactional
    public void execute(AbstractCommonData in, AbstractCommonData inhead, AbstractCommonData out, AbstractCommonData outhead) {
        //0验证用户失败，1验证用户成功
        String username = in.getStringValue("username");
        String password = "";
        try {
            password = AESFactory.encryptString(in.getStringValue("password"));
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(BindUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        //此处查询用户是否存在
        AbstractCommonData userMsg = DataConvertFactory.getInstance();
        userMsg = queryData("query_user", new Object[]{username});
        log.debug("查询返回数据--------->>>{}" + userMsg);

        if (userMsg == null) {
            //密码用户名错误
            log.error("验证用户失败！");
            out.putStringValue("result", "0");
        } else if (userMsg != null) {
            //判断此用户是否已经被绑定
            if (userMsg.getStringValue("openid") == null) {
                //验证密码
                if (password.equals(userMsg.getStringValue("password"))) {
                    log.info("验证用户成功!");
                    out.putStringValue("result", "1");
                    AbstractCommonData session = getSession(in);
                    String userid = session.getStringValue("userid");
//                  //把企业号的成员的id放入user中
                    userMsg.putStringValue("userid", userid);
                    userMsg.remove("password");
                    //在本地新增用户
                    session.putAll(userMsg);
                    //把个人openid存进去
                    update("update_openid_pl_user", new Object[]{userid, username});
                } else {
                    log.info("密码输入不正确!");
                    out.putStringValue("result", "2");
                }
            }else{
                log.info("该用户已被绑定!");
                out.putStringValue("result", "3");
            }

        }

    }
}
